<template>
  <FrontSideTable
    :dataUrl="'/param/paramGroup/paramType/getListByGroup/' + paramGroupId"
    :tableTitle="tableTitle"
    addUrl="/param/paramGroup/paramType/add"
    editUrl="/param/paramGroup/paramType/edit"
    deleteUrl="/param/paramGroup/paramType/delete"
    searchHint="根据名称查找"
    :form="form"
    :rules="rules"
    :searchProps="searchProps"
    :columns="columns"
    @beforeAdd="handleAdd"
    @beforeEdit="handleEdit"
    @onCreated="handleCreated"
    @onBack="handleBack"
    canBack>

    <!--表单-->
    <template slot="form">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="输入名称"></el-input>
      </el-form-item>
      <el-form-item label="顺序号" prop="sortNo">
        <el-input type="number" v-model.number="form.sortNo" placeholder="输入顺序号"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.notes"></el-input>
      </el-form-item>
    </template>

  </FrontSideTable>
</template>

<script>
  import FrontSideTable from "../template/FrontSideTable.vue"
  import {clearFields} from "../../assets/js/utils/ObjectUtils"
  import {intValidator} from "../../assets/js/utils/ValidateUtils"
  import {doGet} from "../../assets/js/utils/RequestUtils"

  let oldName = "";

  export default {
    name:"ParamTypeMaintenance",
    props:{
      tableTitle:{
        type:String,
        required:true
      },
      paramGroupId:{
        type:String,
        required:true
      }
    },
    data() {
      //名称校验
      let nameValidator = (rule, value, callback)=>{
        if(oldName === value) {
          callback();
        } else if(this.dataList.some(item=>item.name === value)) {
          callback(new Error("名称重复"));
        } else {
          callback();
        }
      };

      return {
        dataList:[],
        form:{
          name:"",
          groupId:"",
          notes:"",
          sortNo:0
        },
        searchProps:["name"],
        columns:[
          {prop:"sortNo", label:"顺序号", sortable:"custom", width:"100"},
          {prop:"name", label:"名称"},
          {prop:"notes", label:"备注"}
        ],
        rules:{
          name:[
            {required:true, message:"请输入名称", trigger:"blur"},
            {validator:nameValidator, trigger:"blur"}
          ],
          sortNo:[
            {validator:intValidator, trigger:"blur"}
          ]
        }
      }
    },
    methods:{
      handleCreated(dataList) {
        this.dataList = dataList;
      },
      handleAdd() {
        oldName = "";
        clearFields(this.form);
        this.form.groupId = this.paramGroupId;
      },
      handleEdit(data) {
        oldName = data.name;
        this.form = {...data};
      },
      handleBack() {
        this.$emit("onBack");
      }
    },
    components:{FrontSideTable}
  }
</script>



// WEBPACK FOOTER //
// ParamTypeMaintenance.vue?708bf7a3
